package com.pig.graduate.controller.sys;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig.graduate.domain.sys.SysBuildings;
import com.pig.graduate.domain.sys.SysLog;
import com.pig.graduate.domain.sys.SysUser;
import com.pig.graduate.result.MyResult;
import com.pig.graduate.service.sys.SysLogService;
import com.pig.graduate.service.sys.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(tags = {"操作记录"}, description = "操作记录")
@RestController
@RequestMapping("/sys/log")
public class SysLogController {

    @Autowired
    private SysLogService service;

    @Autowired
    private SysUserService sysUserService;

    /**
     * 操作记录表
     * @return
     */
    @ApiOperation(value = "操作记录列表",notes = "操作记录列表")
    @GetMapping("/list")
    public MyResult logList(@RequestParam("pageNum") Integer pageNum,
                            @RequestParam("pageSize") Integer pageSize){
        SysUser sysUser = new SysUser();
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (!(authentication instanceof AnonymousAuthenticationToken)) {
            String currentUserName = authentication.getName();
            sysUser = sysUserService.getUserByUsername(currentUserName);
        }
        // 查询所有栋舍信息
        Page<SysLog> sysLogPage = new Page<>(pageNum,pageSize);
        Page<SysLog> page;
        if("admin".equals(sysUser.getUsername())){
            page = new LambdaQueryChainWrapper<SysLog>(service.getBaseMapper())
                    .page(sysLogPage);
            return MyResult.success(page);
        }else{
            page = new LambdaQueryChainWrapper<SysLog>(service.getBaseMapper())
                    .eq(SysLog::getUserId,sysUser.getId())
                    .page(sysLogPage);
        }
        return MyResult.success(page);
    }
}
